//产品模块store Product
export default {
    namespaced: true,
    state: {
        //产品数据
        product: []
    },
    mutations: {
        //设置购物车数据
        SET_PRODUCT(state, payload) {
            state.product = payload;
        },
        //让产品数量-1
        INVENTORY_DECREMENT(state, payload) {
            // console.log(payload)
            //去product中找到id对应的数据,然后让inventory字段-1
            let target = state.product.find(item => item.id === payload)

            // console.log(target)
            target.inventory--
        }
    },
    actions: {
        //获取购物车数据
        FETCH_PRODUCT({ commit }, payload) {
            fetch('/api/data/index.json')
                .then(body => body.json())
                .then(res => {
                    // console.log(res.data)
                    //获取异步数据之后直接提交给Mutation
                    commit('SET_PRODUCT', res.data)
                })
        }
    }
}